using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

    /// <summary>
    /// 红点数据管理类
    /// </summary>
    public static class RedDot
    {
        public enum RedDotType
        {
            [InspectorName("红点")]
            red,
            [InspectorName("New(新)")]
            news,
        }
        
        static StringBuilder sb = new StringBuilder();
        
        public class RedDotTreeNode
        {
            private RedDotTreeNode()
            {
            }

            public RedDotTreeNode(string name, RedDotTreeNode parent)
            {
                this.name = name;
                this.parent = parent;
                childs = new();
                count = new();
                systemId = -1;

                if (string.IsNullOrEmpty(name))
                {
                    return;
                }

                sb.Clear();
                while (true)
                {
                    sb.Insert(0, name);

                    if (parent == null)
                    {
                        break;
                    }
                    name = parent.name;
                    if (string.IsNullOrEmpty(name))
                    {
                        break;
                    }
                    sb.Insert(0, '.');
                    parent = parent.parent;
                }

                fullPath = sb.ToString();
                //if (fullPath.Length > 0)
                //{
                //    foreach (var row in ProfileUnlockSystem.GetList())
                //    {
                //        if (fullPath.Equals(row.RedDotPath))
                //        {
                //            systemId = row.ID;
                //            break;
                //        }
                //    }
                //}
            }

            public string name { get; private set; }
            public int systemId { get; private set; }
            public RedDotTreeNode parent { get; private set; }
            public List<RedDotTreeNode> childs { get; private set; }
            public Dictionary<RedDotType, int> count { get; private set; }
            public string fullPath { get; private set; }

            public int Get(RedDotType type)
            {
                var ret = 0;
                if (systemId <= 0)
                {
                    count.TryGetValue(type, out ret);   
                }
                else
                {
                    ret = 0;
                }
                foreach (var c in childs)
                {
                    ret += c.Get(type);
                }

                return ret;
            }

            public void Clear()
            {
                name = "";
                parent = null;
                childs.Clear();
                count.Clear();
            }
        }


        private static RedDotTreeNode Root = new("", null);
        private static Dictionary<IRedDotListener, string> views = new ();

        public static void Init()
        {
            Clear();
            
            //MsgCenter.RegisterMsg(UIMessage.Event_BrocastUnLockedSystem, OnSysUnlocked);
        }

        //static void OnSysUnlocked(object system)
        //{
        //    if(system == null) return;
        //    var systemData = ((int, bool))system;
        //    var unlockSystem = ProfileSystemEnter.Get(systemData.Item1);
        //    if (!string.IsNullOrEmpty(unlockSystem.RedDotPath))
        //    {
        //        Broadcast(unlockSystem.RedDotPath);
        //    }
        //}
        
        public static void Clear()
        {
            Root.Clear();
            views.Clear();
            
            //MsgCenter.UnRegister(UIMessage.Event_BrocastUnLockedSystem, OnSysUnlocked);
        }
        
        static RedDotTreeNode BuildTree(string treePath, RedDotType redDotType)
        {
            if(string.IsNullOrEmpty(treePath)) return null;

            var temp = treePath.Split('.');
            var node = Root;
            var index = 0;
            while (index < temp.Length)
            {
                var nodeName = temp[index];
                var childNode = node.childs.Find(n => n.name.Equals(nodeName));
                if (childNode == null)
                {
                    childNode = new RedDotTreeNode(nodeName, node);
                    node.childs.Add(childNode);
                }

                node = childNode;
                index++;
            }

            node.count.TryAdd(redDotType, 0);

            return node;
        }

        static void Broadcast(string treePath)
        {
            List<IRedDotListener> tempList = new List<IRedDotListener>();
            foreach (var item in views)
            {
                if (treePath.Contains(item.Value))
                {
                    tempList.Add(item.Key);
                }
            }
            
                foreach (var view in tempList)
                {

            view.UpdateView();
        }
        }

        /// <summary>
        /// 设置红点路径的数量
        /// </summary>
        /// <param name="treePath">路径</param>
        /// <param name="redDotType">红点类型</param>
        /// <param name="count">数量</param>
        public static void Set(string treePath, RedDotType redDotType, int count)
        {
            var node = BuildTree(treePath, redDotType);
            if (node == null)
            {
                return;
            }
            node.count[redDotType] = count < 0 ? 0 : count;

            Broadcast(treePath);
        }
        
        /// <summary>
        /// 追加/修改红点路径的数量
        /// </summary>
        /// <param name="treePath"></param>
        /// <param name="redDotType"></param>
        /// <param name="count"></param>
        public static void Append(string treePath, RedDotType redDotType, int count)
        {
            var node = BuildTree(treePath, redDotType);
            if (node == null)
            {
                return;
            }

            var temp = node.count[redDotType] + count;
            node.count[redDotType] = temp < 0 ? 0 : temp;

            Broadcast(treePath);
        }
        
        /// <summary>
        /// 获取红点路径的数量
        /// </summary>
        /// <param name="treePath"></param>
        /// <param name="redDotType"></param>
        /// <returns></returns>
        public static int Get(string treePath, RedDotType redDotType)
        {
            if(string.IsNullOrEmpty(treePath)) return 0;

            var temp = treePath.Split('.');
            var node = Root;
            var index = 0;
            while (index < temp.Length)
            {
                var nodeName = temp[index];
                node = node.childs.Find(n => n.name.Equals(nodeName));
                if (node == null)
                {
                    break;
                }
                index++;
            }

            if (node == null)
            {
                return 0;
            }

            return node.Get(redDotType);
        }

        public static void Register(IRedDotListener view)
        {
            if (view == null)
            {
                return;
            }
            
            UnRegister(view);
            
            var path = view.GetListenPath();
            if(string.IsNullOrEmpty(path))
            {
                return;
            }
            
            views.Add(view, path);
        }
        
        public static void UnRegister(IRedDotListener view)
        {
            views.Remove(view);
        }
    }
